package cn.huiyunche.base.service.interfaces.impl;

import cn.huiyunche.base.service.constant.QiniuConstant;
import cn.huiyunche.base.service.framework.utils.HYCUtils;
import cn.huiyunche.base.service.framework.utils.QiniuUtils;
import cn.huiyunche.base.service.interfaces.UserService;
import cn.huiyunche.base.service.interfaces.UserVehiclesService;
import cn.huiyunche.base.service.mappers.BVehicleBrandMapper;
import cn.huiyunche.base.service.mappers.SUserVehiclesMapper;
import cn.huiyunche.base.service.model.BVehicleBrand;
import cn.huiyunche.base.service.model.SUserVehicles;
import cn.huiyunche.base.service.model.SUserVehiclesExample;
import cn.huiyunche.base.service.vo.UserVehiclesVo;
import cn.huiyunche.base.service.vo.UserVo;
import cn.huiyunche.tools.basic.exceptions.BusinessException;
import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service

public class UserVehiclesServiceImpl implements UserVehiclesService {

    private static final Logger logger = LoggerFactory.getLogger(UserVehiclesServiceImpl.class);

    @Autowired
    private SUserVehiclesMapper sUserVehiclesMapper = null;

    @Autowired
    private BVehicleBrandMapper bVehicleBrandMapper = null;

    @Autowired
    private UserService userService = null;

    @Override
    public int updateUserVehicle(UserVehiclesVo userVehiclesVo) throws Exception {
        logger.info("UserVehiclesServiceImpl.updateUserVehicle param : {}", userVehiclesVo);
        try {
            UserVo user = this.getUserService().getCurrentUserVo();
            userVehiclesVo.setUserid(user.getId());
            SUserVehiclesExample search = new SUserVehiclesExample();
            SUserVehiclesExample.Criteria criteria = search.createCriteria();
            criteria.andUserIdEqualTo(user.getId());
            criteria.andVehicleIdEqualTo(userVehiclesVo.getVehicleid());
            criteria.andBrandIdEqualTo(userVehiclesVo.getBrandid());
            criteria.andIdNotEqualTo(userVehiclesVo.getId());
            List<SUserVehicles> vehicleList = this.getSUserVehiclesMapper().selectByExample(search);
            if (vehicleList.size() > 0) {
                logger.info("The current data already exists : {}", JSONObject.toJSON(vehicleList));
                throw new BusinessException("数据存在！");
            }
            return this.getSUserVehiclesMapper().updateByPrimaryKeySelective(buildUserVehicles(userVehiclesVo));
        } catch (Exception e) {
            logger.error("UserVehiclesServiceImpl.updateUserVehicle error : {}", e);
            throw new BusinessException("更新失败！");
        }
    }

    @Override
    public int addUserVehicles(UserVehiclesVo userVehiclesVo) throws Exception {
        logger.info("UserVehiclesServiceImpl.addUserVehicles param : {}", userVehiclesVo);
        UserVo user = this.getUserService().getCurrentUserVo();
        userVehiclesVo.setUserid(user.getId());
        SUserVehiclesExample search = new SUserVehiclesExample();
        SUserVehiclesExample.Criteria criteria = search.createCriteria();
        criteria.andUserIdEqualTo(userVehiclesVo.getUserid());
        criteria.andVehicleIdEqualTo(userVehiclesVo.getVehicleid());
        criteria.andBrandIdEqualTo(userVehiclesVo.getBrandid());
        List<SUserVehicles> vehicleList = this.getSUserVehiclesMapper().selectByExample(search);
        if (vehicleList.size() > 0) {
            logger.info("The current data already exists : {}", JSONObject.toJSON(vehicleList));
            throw new BusinessException("数据存在！");
        }
        try {
            return this.getSUserVehiclesMapper().insertSelective(buildUserVehicles(userVehiclesVo));
        } catch (Exception e) {
            logger.error("UserVehiclesServiceImpl.addUserVehicles error : {}", e);
            throw new BusinessException("添加失败！");
        }
    }

    @Override
    public int deleteUserVehicles(String vehiclesId) throws Exception {
        logger.info("UserVehiclesServiceImpl.deleteUserVehicles param : {}", vehiclesId);
        return this.getSUserVehiclesMapper().deleteByPrimaryKey(Long.parseLong(vehiclesId));
    }

    @Override
    public List<SUserVehicles> selectUserVehicles() throws Exception {
        UserVo user = userService.getCurrentUserVo();
        logger.info("UserVehiclesServiceImpl.selectUserVehicles userid : {}", user);
        SUserVehiclesExample search = new SUserVehiclesExample();
        SUserVehiclesExample.Criteria userVehicles = search.createCriteria();
        userVehicles.andUserIdEqualTo(user.getId());
        List<SUserVehicles> userVehiclelists = this.getSUserVehiclesMapper().selectByExample(search);
        for (SUserVehicles brand : userVehiclelists) {
            String strUrl = QiniuUtils.generateDownloadTicket(
                    HYCUtils.getConstantPropertiesLoader().getProperty(QiniuConstant.QINIU_LOGO_BUCKET),
                    brand.getBrandLogo(), "", "", "");
            brand.setBrandLogo(strUrl);
        }
        return userVehiclelists;
    }

    private SUserVehicles buildUserVehicles(UserVehiclesVo userVehiclesVo) {
        SUserVehicles userVehicles = new SUserVehicles();
        userVehicles.setId(userVehiclesVo.getId());
        userVehicles.setUserId(userVehiclesVo.getUserid());
        userVehicles.setBrandId(userVehiclesVo.getBrandid());
        userVehicles.setBrandName(userVehiclesVo.getBrandname());
        userVehicles.setVehicleId(userVehiclesVo.getVehicleid());
        userVehicles.setVehicleName(userVehiclesVo.getVehiclename());

        BVehicleBrand vehiclebrand = this.getBVehicleBrandMapper().selectByPrimaryKey(userVehiclesVo.getBrandid());
        userVehicles.setBrandLogo(vehiclebrand.getBrandLogo());

        userVehicles.setVehicleClass(userVehiclesVo.getVehicleclass());
        userVehicles.setVehicleLengh(userVehiclesVo.getVehiclelengh());
        userVehicles.setVehicleWidth(userVehiclesVo.getVehiclewidth());
        userVehicles.setVehicleHeight(userVehiclesVo.getVehicleheight());
        userVehicles.setVehicleSelfweight(userVehiclesVo.getVehicleselfweight());
        userVehicles.setComment(userVehiclesVo.getComment());
        userVehicles.setUpdateTime(new Date());
        return userVehicles;
    }

    private SUserVehiclesMapper getSUserVehiclesMapper() {
        return this.sUserVehiclesMapper;
    }

    private BVehicleBrandMapper getBVehicleBrandMapper() {
        return this.bVehicleBrandMapper;
    }

    private UserService getUserService() {
        return userService;
    }
}
